/**
 * Copyright (C) 2006 - present dabuTech Corporation  
 * All Rights Reserved.
 *
 * This file is part of Easier Java Persistence.
 *
 * EJP is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
 * FITNESS FOR A PARTICULAR PURPOSE. See the accompanying license 
 * for more details.
 *
 * You should have received a copy of the license along with EJP; if not, 
 * go to http://www.EasierJava.com and download the latest version.
 */

package com.test;

import ejp.Database;
import java.util.ArrayList;
import java.util.List;
import ejp.DatabaseManager;
import ejp.DatabaseException;
import ejp.Result;



public class TExample
  {
	public TExample(DatabaseManager dbm) throws DatabaseException {
		// Clean out contacts
		dbm.executeUpdate("delete from t2");

//		// Inserting contact with associations
		T2 t2 = new T2(0, 1);
		t2.getT3().add(new T3(0,t2.getId(),2));
		t2.getT3().add(new T3(0,t2.getId(),3));
		t2.getT3().add(new T3(0,t2.getId(),4));
		t2.getT3().add(new T3(0,t2.getId(),5));
		t2.getT3().add(new T3(0,t2.getId(),6));

		dbm.saveObject(t2);

		Database db = dbm.getDatabase();

		try {
			Result<T2> result = db.queryObject(T2.class, "order by :id");
			for (T2 t22 : result) {
				System.out.println(t22);
				if (t22.getId() == 1) {
					for (T3 t33 : t22.getT3())
						System.out.println("t22.getVv== " + t33.getVv());
				}
			}
			result.close();
		}catch(Exception e){
			e.printStackTrace();
		} finally {
			// also closes any open results
			db.close();
		}
	}

//    static DataSource getDbcpDataSource() throws ClassNotFoundException
//      {
//        Class.forName("org.hsqldb.jdbcDriver");
//
//        GenericObjectPool connectionPool = new GenericObjectPool(null);
//        ConnectionFactory connectionFactory = new DriverManagerConnectionFactory("jdbc:hsqldb:file:/hsqldb/jdbc/ejp", null, null);
//        PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,connectionPool,null,null,false,true);
//        return new PoolingDataSource(connectionPool);
//      }

    public static void main(String[] args) throws DatabaseException, ClassNotFoundException
      {
        DatabaseManager dbm = null;

        try
          {
           // DatabaseManager.setLogLevel(Level.WARNING);
            /** 数据库管理对象 */
//        	dbm = DatabaseManagerFactory.getDatabaseManager();
        	
//            dbm = MemoryDatabase.getMemoryDatabase();
//            dbm = DatabaseManager.getDatabaseManager("ejp");
//            dbm = DatabaseManager.getDatabaseManager("ejp", 10, getDbcpDataSource());//org.gjt.mm.mysql.Driver//com.mysql.jdbc.Driver
            dbm = DatabaseManager.getDatabaseManager("test", 10, "org.gjt.mm.mysql.Driver", "jdbc:mysql://localhost/test","root","tigerlei");

            for (int i = 0; i < 1; i++)
              {
                new TExample(dbm);
                System.out.println("count=" + (i + 1));
                System.gc();
              }
          }
        finally
          {
            // also closes any open ejp.Database
            dbm.close();
          }
      }
    
	public static class T2 {

		private static final long serialVersionUID = 100L;

		private int id, tid;
		private List<T3> t3 = new ArrayList<T3>();

		T2(int id, int tid) {
			this.id = id;
			this.tid = tid;
		}

		public int getId() {
			return id;
		}

		public void setId(int id) {
			this.id = id;
		}

		public int getTid() {
			return tid;
		}

		public void setTid(int tid) {
			this.tid = tid;
		}

		public List<T3> getT3() {
			return t3;
		}

		public void setT3(List<T3> t3) {
			this.t3 = t3;
		}

	}

	public static class T3 {
		private static final long serialVersionUID = 100L;

		private Integer id, vv,ss;

		T3(int ss) {
			this.ss = ss;
		}

		T3(int id,int ss) {
			this.ss = ss;
			this.id = id;
		}
		T3(int id,int vv,int ss) {
			this.ss = ss;
			this.id = id;
			this.vv = vv;
		}
		public Integer getSs() {
			return ss;
		}

		public void setSs(Integer ss) {
			this.ss = ss;
		}

		public Integer getId() {
			return id;
		}

		public void setId(Integer id) {
			this.id = id;
		}

		public Integer getVv() {
			return vv;
		}

		public void setVv(Integer vv) {
			this.vv = vv;
		}

	}
    
  }
